﻿using System;
using System.Collections;
using System.Collections.Generic;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.View.Base;

namespace HR60_PanelSoft.View.TabForms.ListItems
{
	public partial class PinItem : BaseFormControl
	{
		// 运行时长最大值
		private static readonly int TIME_MAX = 30;

		private Pin pin;

		public Pin TagPin
		{
			get
			{
				return pin;
			}
		}

		public PinItem(Pin pin, string name)
		{
			InitializeComponent();
			this.pin = pin;

			// 查找关联的设备及分组
			Device device = null;
			Device sub = null;
			findDeviceByUuid(pin.connectedDevice, ref device, ref sub);
			DeviceGroup group = findGroupByDeviceUuid(pin.connectedDevice);

			// 标题
			ltlName.Text = name;

			// 设备分组
			selGroup.setStaticFirstItem("选择设备分组");
			selGroup.bindData((IList) deviceGroups, "name");
			if(group != null) selGroup.Value = group;
			selGroup.ValueChanged += new EventHandler(selGroup_ValueChanged);

			// 设备
			selDevice.setStaticFirstItem("选择设备");
			refreshDeviceList(group, device);
			selDevice.ValueChanged += new EventHandler(selDevice_ValueChanged);

			// 分机
			selSub.setStaticFirstItem("选择分机");
			refreshSubList(device, sub);
			selSub.ValueChanged += new EventHandler(valueChange);

			// 时长
			if (pin.type == PinType.L || pin.type == PinType.T)
			{
				for (int i = 0; i <= TIME_MAX; i++)
				{
					selTime.Items.Add(i);
				}
				resetTimeSel();
				selTime.Value = ((LoadPin)pin).time;
				selTime.ValueChanged += new EventHandler(valueChange);
			}
		}



		// 刷新设备列表，同时联动运行时长
		private void refreshDeviceList(DeviceGroup group, Device selectedDevice)
		{
			// 刷新设备列表
			IList<Device> list = new List<Device>();
			if (group != null)
			{
				foreach (Device dev in group.devices)
				{
					if (
						(pin.type == dev.type.pinType) ||
						(pin.type == PinType.T && dev.type.pinType == PinType.LT) ||
						(pin.type == PinType.L && dev.type.pinType == PinType.LT) ||
						(
							dev.type.subDeviceType != null &&
							(
								(pin.type == dev.type.subDeviceType.pinType) ||
								(pin.type == PinType.T && dev.type.subDeviceType.pinType == PinType.LT) ||
								(pin.type == PinType.L && dev.type.subDeviceType.pinType == PinType.LT))
							)
						)
					{
						list.Add(dev);
					}
				}
			}
			selDevice.bindData((IList)list, "nickName");
			selDevice.Value = selectedDevice;
		}


		// 刷新分机列表
		private void refreshSubList(Device device, Device selectedSub)
		{
			// 刷新设备列表
			if (device == null || device.type.subDeviceType == null)
			{
				selSub.Visible = false;
				return;
			}
			selSub.Visible = true;
			selSub.bindData((IList)device.childDevices, "nickName");

			// 选中目标设备
			if (selectedSub != null)
			{
				selSub.Value = selectedSub;
			}
		}

		// 分组选择事件
		void selGroup_ValueChanged(object sender, EventArgs e)
		{
			selDevice.CatchValueChange = false;
			selSub.CatchValueChange = false;
			selTime.CatchValueChange = false;

			refreshDeviceList((DeviceGroup)selGroup.Value, null);
			refreshSubList((Device)selDevice.Value, null);
			resetTimeSel();

			selDevice.CatchValueChange = true;
			selSub.CatchValueChange = true;
			selTime.CatchValueChange = true;
			valueChange(this, e);
		}

		// 设备选择事件
		void selDevice_ValueChanged(object sender, EventArgs e)
		{
			selSub.CatchValueChange = false;
			selTime.CatchValueChange = false;

			refreshSubList((Device)selDevice.Value, null);
			resetTimeSel();

			selSub.CatchValueChange = true;
			selTime.CatchValueChange = true;
			valueChange(this, e);
		}

		private void resetTimeSel()
		{
			if (selDevice.Value != null)
			{
				// 开合类显示运行时间
				if (((Device)selDevice.Value).type.switchTypes.Contains(SwitchType.OPEN_JOIN))
				{
					selTime.Visible = true;
					selTime.Value = ((LoadPin)pin).time;
					ltlTime.Visible = true;
				}
				else
				{
					selTime.Visible = false;
					ltlTime.Visible = false;
				}
			}
			else
			{
				selTime.Visible = false;
				ltlTime.Visible = false;
			}
		}

		// 重写Value属性
		public override object Value
		{
			get
			{
				IDictionary<string, object> res = new Dictionary<string, object>();
				res.Add("device", selDevice.Value);
				res.Add("time", selTime.Value);
				return res;
			}
			set
			{
				throw new Exception("read only");
			}
		}
	}
}
